<?php

namespace app\index\controller;

use app\common\lib\redis\Predis;
use app\common\lib\Redis;
use app\common\lib\Util;

class Index
{
    public function index()
    {
        echo 1122;
    }

    public function login()
    {

        $phoneNum = request()->get('phone_num',0,'intval');
        $code = request()->get('code',0,'intval');

        if(empty($phoneNum) || empty($code)){
            return Util::show(config('code.error'),'参数错误');
        }

        // 获取存入的code
        try{
            $redisCode = Predis::getInstance()->get(Redis::smsKey($phoneNum));
        }catch(\Exception $e){

        }
        if($redisCode == $code){
            // 重置redis
            Predis::getInstance()->set(Redis::smsKey($phoneNum),'');
            $data = [
                'user' => $phoneNum,
                'srcKey' => md5(Redis::userKey($phoneNum)),
                'time' => time(),
                'isLogin' => true,
            ];
            Predis::getInstance()->set(Redis::userKey($phoneNum),$data);
            return Util::show(config('code.success'),'ok',$data);
        }else{
            return Util::show(config('code.error'),'login error');
        }

    }


}
